layui.use(['form', 'table', 'layuimini', 'element'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var BoMusic = {
        id: 'BoMusicTable'
    };

    // 当前弹出层，防止ID被覆盖
    var parentIndex = layer.index;

    var column = [
        [
            {type: 'radio'},
            {field: 'title', title: '歌名', align: 'center'},
            {field: 'artist', title: '歌手名', align: 'center'},
            {field: 'poster', title: '封面图', align: 'center',
                templet: function (object) {
                    if(object.poster == '' || object.poster == null){
                        return '-';
                    }else{
                        return '<img src="/layuimini/images/' + object.poster +'"';
                    }
                }
            },
            {field: 'isShow', title: '是否显示', align: 'center', width: 100,
                templet: function (object) {
                    if(object.isShow == 0){
                        return '<input type="checkbox" lay-filter="l_isShowSwitch" lay-skin="switch" lay-text="是|否" value="' + object.id + '" checked disabled>';
                    }else{
                        return '<input type="checkbox" lay-filter="l_isShowSwitch" lay-skin="switch" lay-text="是|否" value="' + object.id + '" disabled>';
                    }
                }
            },
            {field: 'sort', title: '排序', align: 'center'},
            {field: 'size', title: '音乐大小', align: 'center',
                templet: function (object) {
                    var size = object.size / 1024 / 1024;
                    return size.toFixed(3) + 'M';
                }
            },
            {field: 'updateDate', title: '更新时间', minWidth: 120, align: 'center'},
            {field: 'createDate', title: '上传时间', minWidth: 120, align: 'center'}
        ]
    ];

    table.render({
        elem: '#' + BoMusic.id,
        url: '/web/boMusic/list',
        toolbar: '#in_toolbarBtn',
        defaultToolbar: [],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    // 监听搜索操作
    $("#in_data-search-btn").on('click', function () {

        //获取搜索条件
        var title = $("#search_title").val();
        var artist = $("#search_artist").val();
        var isShow = $("#search_isShow").val();

        //封装
        var dataObject = {};
        dataObject['title'] = title;
        dataObject['artist'] = artist;
        dataObject['isShow'] = isShow;
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoMusic.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    // 监听重置操作
    $("#in_data-reset-btn").on('click', function () {

        //初始化
        $("#search_title").val("");
        $("#search_artist").val("");
        $("#search_isShow").val("-1");

        form.render();

        //封装
        var dataObject = {};
        dataObject['title'] = "";
        dataObject['artist'] = "";
        dataObject['isShow'] = "-1";
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoMusic.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });


    // 监听确定选择操作
    $(".layuimini-container").on("click", "#in_choose-btn", function () {
        var musicId = $("#in_musicId").val();
        var musicName = $("#in_musicName").val();
        $("#musicId").val(musicId);
        $("#musicName").val(musicName);
        // 关闭弹出层
        layer.close(parentIndex);
        return true;
    });


    //监听表格单选按钮
    table.on('radio(BoMusicTable)', function (obj) {
        $("#in_musicId").val(obj.data.id);
        $("#in_musicName").val(obj.data.title);
    });

});